﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var file = "c:\\dat.rar";
            string l = "120 100 100";
            string a = "0";
            if (args.Length >= 1) file = args[0];
            if (args.Length >= 2) l = args[1];
            if (args.Length >= 3) a = args[2];
            Console.WriteLine(file);
            Console.WriteLine(l);
            Console.WriteLine(a);
            if (File.Exists(file))
            {
                Directory.CreateDirectory("c:\\dat");
                var dir = "c:\\dat\\" + DateTime.Now.ToString().Replace(':','_');
                Directory.CreateDirectory(dir);

                var psi = new ProcessStartInfo();

                string ex = "";
                if (File.Exists("C:\\Program Files\\WinRAR\\WinRAR.exe"))
                    psi = new ProcessStartInfo( "c:\\Program Files\\WinRAR\\WinRAR.exe", "x \"" + file + "\" *.* \"" + dir + "\\\" -y");   
                else 


                psi = new ProcessStartInfo("c:\\Program Files\\7-Zip\\7z.exe", "x \"" + file + "\" -y -o\"" + dir + "\"");
                psi.UseShellExecute = false;
                Console.WriteLine();
                Console.WriteLine(ex);
                var process = Process.Start(psi);
                while (!process.HasExited) Thread.Sleep(1000);
            
                foreach (var VARIABLE in Directory.GetFiles(dir, "*.dat"))
                {
                    try
                    {
                        if (File.Exists(dir + "\\" + Path.GetFileNameWithoutExtension(VARIABLE) + ".ann"))
                        {
                            Directory.CreateDirectory(dir + "\\temp");
                            File.Copy(dir + "\\" + Path.GetFileNameWithoutExtension(VARIABLE) + ".ann",dir+"\\temp\\" + Path.GetFileNameWithoutExtension(VARIABLE) + " 0 1.ann");
                        }
                        var s = File.ReadAllLines(VARIABLE)[0];
                        s = s.Split(' ')[1] + " " + l + " " + s.Split(' ')[2];
                        var ps = new ProcessStartInfo("Сlassifier Tool For OpenCV.exe",
                            "-fannfile \"" + VARIABLE + "\" -fannlayers \"" + s + "\" -fannepoch 2000 -fannalgoritm "+a+" -fannrun");
                        psi.UseShellExecute = false;
                    //    process.PriorityClass = ProcessPriorityClass.Idle;
                        var p = Process.Start(ps);
                    }
                    catch(Exception exd)
                    {
                      Console.WriteLine( exd.Message);
                    }

                }
            }


        }
    }
}
